package com.jt.www.admin.stats.service.impl;

import com.alibaba.fastjson.JSON;
import com.jt.www.admin.stats.service.BigScreenStatsService;
import com.jt.www.common.service.PolicyAttributeService;
import com.jt.www.domain.vo.EduDataStatisticVO;
import com.jt.www.enums.ErrorCode;
import com.jt.www.enums.ResultEnum;
import com.jt.www.model.policyattribute.vo.PlicyClassVO;
import com.jt.www.model.reps.ResponseData;
import com.jt.www.model.reps.ResultDTO;
import com.jt.www.model.vo.stats.bigscreen.accidenttype.AccTypeParamVO;
import com.jt.www.model.vo.stats.bigscreen.accidenttype.SzxAccTypeResultVO;
import com.jt.www.model.vo.stats.bigscreen.accidenttype.XzxAccTypeResultVO;
import com.jt.www.model.vo.stats.bigscreen.closecase.CloseCaseParamVO;
import com.jt.www.model.vo.stats.bigscreen.closecase.CloseCaseResultVO;
import com.jt.www.model.vo.stats.bigscreen.incidentrate.IncidentRateParamVO;
import com.jt.www.model.vo.stats.bigscreen.incidentrate.IncidentRateResultVO;
import com.jt.www.model.vo.stats.bigscreen.insrate.InsRateParamVO;
import com.jt.www.model.vo.stats.bigscreen.insrate.InsRateResultVO;
import com.jt.www.model.vo.stats.bigscreen.riskperiod.RiskPeriodParamVO;
import com.jt.www.model.vo.stats.bigscreen.riskperiod.SzxRiskPeriodResultVO;
import com.jt.www.model.vo.stats.bigscreen.riskperiod.XzxRiskPeriodResultVO;
import com.jt.www.model.vo.stats.bigscreen.schoolrisk.SchoolRiskParamVo;
import com.jt.www.model.vo.stats.bigscreen.schoolrisk.SchoolRiskResultVo;
import com.jt.www.model.vo.stats.bigscreen.schoolriskhide.SchoolRiskHiddenParamVO;
import com.jt.www.model.vo.stats.bigscreen.schoolriskhide.SchoolRiskHiddenResultVO;
import com.jt.www.remote.ins.InsClient;
import com.jt.www.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin_ltf
 * @create 2019-05-18 19:25
 */
@Service
public class BigScreenStatsServiceImpl implements BigScreenStatsService {

    @Autowired
    private InsClient insClient;
    @Autowired
    private PolicyAttributeService policyAttributeService;


    @Override
    public ResultDTO searchSzxAccidentTypePro(AccTypeParamVO accTypeParamVO) {

        List<SzxAccTypeResultVO> list = new ArrayList<>();
        SzxAccTypeResultVO vo = new SzxAccTypeResultVO();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO searchXzxAccidentTypePro(AccTypeParamVO accTypeParamVO) {
        List<XzxAccTypeResultVO> list = new ArrayList<>();
        XzxAccTypeResultVO vo = new XzxAccTypeResultVO();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO searchSzxRiskPeriod(RiskPeriodParamVO riskPeriodParamVO) {
        List<SzxRiskPeriodResultVO> list = new ArrayList<>();
        SzxRiskPeriodResultVO vo = new SzxRiskPeriodResultVO();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO searchXzxRiskPeriod(RiskPeriodParamVO riskPeriodParamVO) {
        // XzxRiskPeriodResultVO
        List<XzxRiskPeriodResultVO> list = new ArrayList<>();
        XzxRiskPeriodResultVO vo = new XzxRiskPeriodResultVO();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO searchInsRateInfo(InsRateParamVO insRateParamVO) {
        // InsRateResultVO
        List<InsRateResultVO> list = new ArrayList<>();
        InsRateResultVO vo = new InsRateResultVO();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO searchCloseCasePer(CloseCaseParamVO closeCaseParamVO) {
        // CloseCaseResultVO
        List<CloseCaseResultVO> list = new ArrayList<>();
        CloseCaseResultVO vo = new CloseCaseResultVO();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO searchIncidentRate(IncidentRateParamVO incidentRateParamVO) {
        // IncidentRateResultVO
        List<IncidentRateResultVO> list = new ArrayList<>();
        IncidentRateResultVO vo = new IncidentRateResultVO();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO searchSchoolRisk(SchoolRiskParamVo schoolRiskParamVo) {
        // SchoolRiskResultVo
        List<SchoolRiskResultVo> list = new ArrayList<>();
        SchoolRiskResultVo vo = new SchoolRiskResultVo();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO searchSchRiskHide(SchoolRiskHiddenParamVO schoolRiskHiddenParamVO) {
//        SchoolRiskHiddenResultVO
        List<SchoolRiskHiddenResultVO> list = new ArrayList<>();
        SchoolRiskHiddenResultVO vo = new SchoolRiskHiddenResultVO();
        list.add(vo);
        HashMap map = new HashMap<String,Object>();
        map.put("resultList",list);
        return new ResultDTO(ResultEnum.SUCCESS,map);
    }

    @Override
    public ResultDTO insData(Map<String, String> conditions) {

        try {
            ResponseData responseData = insClient.eduDataStatistic(JsonUtil.object2Json(conditions));
            if(ErrorCode.SUCCESS_CODE_0000.equals(responseData.getCode())){

                //获取投保中心返回结果对象
                List<EduDataStatisticVO> list = (List<EduDataStatisticVO>) responseData.getList();
                list = JSON.parseArray(JSON.toJSONString(list),EduDataStatisticVO.class);

                //如果结果不为空，计算投保学生率 投保学生率 = 投保学生数 / 该地区学生总数
                if(list.size() > 0){
                    if(list.get(0).getStudentsNum() != null){

                        StringBuilder type = new StringBuilder("");
                        if(!StringUtils.isEmpty(conditions.get("provinceCode"))){
                            type.append(conditions.get("provinceCode"));
                        }
                        if(!StringUtils.isEmpty(conditions.get("cityCode"))){
                            type.append(",").append(conditions.get("cityCode"));
                        }
                        if(!StringUtils.isEmpty(conditions.get("countyCode"))){
                            type.append(",").append(conditions.get("countyCode"));
                        }

                        List<PlicyClassVO> policyAttributes = policyAttributeService.getProInfo("dpxsrs", type.toString(),"0");
                        if(policyAttributes != null && policyAttributes.size() > 0){
                            BigDecimal rate = new BigDecimal(list.get(0).getStudentsNum()).divide(new BigDecimal(policyAttributes.get(0).getCode())).multiply(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP);
                            list.get(0).setRate(rate);
                        }
                    }
                }
                return new ResultDTO(ResultEnum.SUCCESS_0000,list.get(0));
            }
            return new ResultDTO(ResultEnum.ERROR_9999,"投保中心投保数据查询异常");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(ResultEnum.ERROR_9999,"投保中心投保数据查询异常");
        }
    }
}
